//* Copyright(C) 2005-2014, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
* 工具栏插件类；
*/
define(function (require, exports, module) {
	
	WebPDF.RIBBON_TOOLBAR_PLUGIN_NAME = "ToolBar Ribbon Plugin";
    /**
     * Plugin of ribbon tool bar.
     * @param {WebPDF.ReaderApp} readerApp app instance
     * @extends {WebPDF.IReaderPlugin}
     * @constructor
     */
    WebPDF.RibbonToolBarPlugin = function (readerApp) {
        var _readerApp = readerApp,
            _isInitialized = false,
            _language = _readerApp.getLanguage();
           // _self = this;

        var _ribbonDivID = _readerApp.getTopPanelID()+"_ribbon";
        var _topBar1DivID = _ribbonDivID + "_topbar1";
        var _topBar2DivID = _ribbonDivID + "_topbar2";
        var _leftToolBarDivID = _topBar2DivID+"_dockButtons";
        var _rightToolBarDivID = _topBar2DivID+ "_righttools";

        //获取Plugin名称;
        this.getPluginName = function () {
            return WebPDF.RIBBON_TOOLBAR_PLUGIN_NAME;
        };

        //插件被注册的时候调用;
        this.onRegister = function () {
            if (!WebPDF.Config.ToolBar){
                return false;
            }
            if (_isInitialized){
                return true;
            }
            _createCmdAndCmdUI();
            var jqTopPanel = $("#" + _readerApp.getTopPanelID());
            var ribbonPanelDiv = "<div id='"+_ribbonDivID+"' class='fwr_ribbon_top_panel' />";
            jqTopPanel.append(ribbonPanelDiv);
            var jqRibbonDiv = $("#"+_ribbonDivID);
            var topBar1Div = "<div id='" + _topBar1DivID + "' class='fwr_ribbon_topbar1'/>";
            jqRibbonDiv.append(topBar1Div);
            var jqTopBar1 = $("#"+_topBar1DivID);
            var topBar2Div = "<div id='" + _topBar2DivID + "' class='fwr_ribbon_topbar2'/>";
            jqRibbonDiv.append(topBar2Div);
            var jqTopBar2 = $("#"+_topBar2DivID);
            
            var fileNameDivID = _topBar1DivID+ "_filename";
            var fileNameDiv = "<span id='" + fileNameDivID + "' class='fwr-file-name'>" + _readerApp.getFileName() + "</span>" ;
            jqTopBar1.append(fileNameDiv);
            var toolBarButtonDockDiv = "<span class='fwr_ribbon_topbar2_dockbutton fwr-text-upperCase fwr_ribbon_left_toolbar' id='"
                +_leftToolBarDivID+"' />";
            jqTopBar2.append(toolBarButtonDockDiv);
            var rightToolBarDiv = "<span class='fwr_ribbon_right_toolbar' id='"+_rightToolBarDivID+"' />";
            jqTopBar2.append(rightToolBarDiv);
            var brandDivId = _topBar1DivID + "_brand";
            var brandDiv = "<span class='fwr_ribbon_topbar1_container fwr_ribbon_topbar1_rowleft' id='"+brandDivId+"'>" +
            					"<a style='width:16px;height:16px'> <img width='16' height='16' style='border:none; vertical-align:middle;margin-top:-3px;' src='images/foxitappicon.png' /> </a>" +
            					"<span id='FoxitPDFWebReaderBrand' class='fwr_foxit_brand'>Foxit Web Reader</span>"+
            				"</span>";
          
            jqTopBar1.append(brandDiv);
            jqTopPanel.show();
        };

        //模块初始化;
        this.init = function () {
            if (!WebPDF.Config.ToolBar)
                return false;
            if (_isInitialized)
                return true;
            var jqRightToolBar = $("#"+_rightToolBarDivID);
            jqRightToolBar.append(_createRightToolBarHtml(false));
            var jqLeftToolBar = $("#"+_leftToolBarDivID);
            jqLeftToolBar.append(_createRightToolBarHtml(true));
            _initToolBarUI(false);
            _initToolBarUI(true);
            $('#'+ _readerApp.getMainView().getMainFrameID()).on('mousedown', '.fwr_toolbar_btn', function (event) {
                try {
                    if (event.button == 0 || event.button == 1) {
                        var cmdName = event.target.getAttribute("cmd");
                        _readerApp.execCommand(cmdName, null);
                    }
                }
                catch (ex) {
                    console.error(ex);
                }
            });
            _isInitialized = true;
        };

        function _createRightToolBarHtml(isLeft) {
        	var RibbonToolBar = WebPDF.Config.RibbonToolBar;
        	var tools = isLeft ? RibbonToolBar.left : RibbonToolBar.right;
        	var toolsNum = tools.length;
        	var toolbarHtml = "";
        	for(var i = 0; i < toolsNum; i++)
        	{
        		 var btnName = tools[i];
                 var commandUI = _readerApp.getCommandUI(btnName);
                 if (!commandUI)
                     continue;
                 toolbarHtml += commandUI.createHtml();
        	}
            return toolbarHtml;
        }
        
        function _initToolBarUI(isLeft) {
            var RibbonToolBar = WebPDF.Config.RibbonToolBar;
            var tools = isLeft ? RibbonToolBar.left : RibbonToolBar.right;
            var toolsNum = tools.length;
        	for(var i = 0; i < toolsNum; i++){
        		 var btnName = tools[i];
                 var commandUI = _readerApp.getCommandUI(btnName);
                 if (!commandUI)
                     continue;
                commandUI.init(_readerApp);
        	}
        }

        function _createCmdAndCmdUI() {
            var appId = _readerApp.getAppId();
            var qrCodeId = appId + "_fx_doc_qrcode";
            var qrCodeDocImageId = appId + "_fx_doc_qrcode_image";
            var qrCodeImageId = appId + "_fwr-qrcode-image";
            //添加分割符;
            _readerApp.addCommandUI({
                name: "ToolBarSeparator",
                commandName: null,
                createHtml: function () {
                    return "<div class='fwr_toolbar_separator'></div>";
                }
            });
            _readerApp.addCommandUI({
                name: "QRCode",
                commandName: null,
                createHtml: function () {
                    return "<div class='fwr_toolbar_btn fwr-qrcode-frame fwr_toolbar_btn_margin' id='" + qrCodeId
                        + "' style='padding-left: 0px;padding-right: 0px;'><a class='fwr-qrcode'/></div>";
                },
                init: function () {
                	var jqMainFrm = $("#" + _readerApp.getMainView().getMainFrameID());
                	jqMainFrm.append("<div class='fwr-qrcode-image-div fwr-hidden' id='" + qrCodeDocImageId + "'><img id='"
                        + qrCodeImageId + "' alt='' src='' class='fwr-qrcode-image'/></div>");
                	var imgUrl = WebPDF.Config.Url.QrcodeUrl;
                    var jqQrCodeImage = $("#" + qrCodeImageId);
                    jqQrCodeImage.attr("src", imgUrl);
                	$("#" + qrCodeId).on('mouseenter', function () {
                		WebPDF.Common.slideToggle("#" + qrCodeDocImageId,true);
                       return true;
                    }).on('mouseleave', function () {
                        WebPDF.Common.slideToggle("#" + qrCodeDocImageId,false);
                        return true;
                    });
                }
            });
            var fileOpenBtnId = appId + "_btnFileOpen";
            _readerApp.addCommandUI({
                name: "FileOpen",
                commandName: 'FileOpen',
                state: false,
                createHtml: function () {
                	 return "<a id='" + fileOpenBtnId + "' style='margin-left: 1px;' class='fwr_toolbar_btn fwr_toobar_fileopen fwr_toolbar_text_btn' cmd=''>"
                     +"<span class='fwr_toollabel' style='color:#fff;'>" + _language.CommandUI.FileOpen + "</span></a>";           
                },
                onCommandStateChange: function (data) {
                },
                init: function () {
                }
            });
        }
        //模块卸载;
        this.unload = function () {
        };

        this.getReaderApp = function () {
            return _readerApp;
        };
    };

    exports.createPlugin = function (readerApp) {
        var toolBarPlugin = new WebPDF.RibbonToolBarPlugin(readerApp);
        return toolBarPlugin;
    };
});